package com.demo.juc.cas;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2022/10/12 15:50
 * @Version: 1.0
 */
public class CasDemo01 {

    public static void main(String[] args) {

        AtomicInteger atomicInteger = new AtomicInteger(2020);//原子类

        //对我们写平时写的sql:乐观锁
        //如果和期望的值相同就更新，CAS是CPU的并发原语
        //*******捣乱的线程**********
        System.out.println(atomicInteger.compareAndSet(2020, 2021));
        System.out.println(atomicInteger.get()); // 2021
        System.out.println(atomicInteger.compareAndSet(2021, 2020));
        System.out.println(atomicInteger.get()); // 2021
        //********期望的线程*********
        System.out.println(atomicInteger.compareAndSet(2020, 2021));
        System.out.println(atomicInteger.get());//2021

    }
}
